package com.epfresh.views.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.bean.Dc;
import com.epfresh.bean.DeliveryDate;

import java.util.ArrayList;
import java.util.List;

public class DeliveryDateDialog extends Dialog implements View.OnClickListener {
    private View root;
    private ListView lv;
    private Button rButton, lButton;

    private BaseAdapter adapter;

    private List<DeliveryDate> markets = new ArrayList<>();

    private OnPositiveClickListener onPositiveClickListener;
    private Context mContext;
    private Object o;

    private int currentIndex = -1;

    public DeliveryDateDialog(Context context) {
        super(context);
        mContext = context;

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(true);


        root = getLayoutInflater().inflate(R.layout.dialog_market, null);
        lv = (ListView) root.findViewById(R.id.lv);

        rButton = (Button) root.findViewById(R.id.positiveButton);
        rButton.setOnClickListener(this);
        lButton = (Button) root.findViewById(R.id.negativeButton);
        lButton.setOnClickListener(this);


        setTitle("选择配送时间");
        setContentView(root);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = LocalDisplay.SCREEN_WIDTH_PIXELS;
        dialogWindow.setAttributes(lp);


        adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return markets.size();
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder vh = null;
                if (convertView == null) {
                    convertView = getLayoutInflater().inflate(R.layout.item_list_market, null);
                    vh = new ViewHolder();
                    vh.tv = (TextView) convertView.findViewById(R.id.tv_content);
                    convertView.setTag(vh);
                } else {
                    vh = (ViewHolder) convertView.getTag();
                }

                DeliveryDate market = markets.get(position);

                vh.tv.setText(market.getTitle());

                if (position == currentIndex){
                    vh.tv.setTextColor(Color.parseColor("#f13d1a"));
                }else {
                    vh.tv.setTextColor(Color.parseColor("#434343"));
                }

                return convertView;
            }
        };

        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onPositiveClickListener != null) {
                    onPositiveClickListener.onPositiveClickListener(o, markets.get(position));
                }
                if (isPositiveDismiss)
                    dismiss();
            }
        });

    }

    public List<DeliveryDate> getMarkets() {

        return markets;
    }

    public void setMarkets(List<DeliveryDate> markets) {
        if (markets == null){
            return;
        }
        this.markets.clear();
        this.markets.addAll(markets);
        currentIndex = -1;
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    public void setCurrent(List<DeliveryDate> markets,String marketId){
        setMarkets(markets);
        if (markets != null && marketId != null && !"".equals(marketId)){
            for (int i=0;i<markets.size();i++){
                DeliveryDate market = markets.get(i);
                if (marketId.equals(market.getId())){
                    currentIndex = i;
                    if (adapter != null){
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        }
    }

    public Object getO() {
        return o;
    }

    public void setO(Object o) {
        this.o = o;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.positiveButton:

                if (isPositiveDismiss)
                    dismiss();
                if (onPositiveClickListener != null) {


                }
                break;
            case R.id.negativeButton:
                dismiss();
                break;
        }
    }


    public OnPositiveClickListener getOnPositiveClickListener() {
        return onPositiveClickListener;
    }

    public void setOnPositiveClickListener(OnPositiveClickListener onPositiveClickListener) {
        this.onPositiveClickListener = onPositiveClickListener;
    }

    public void setTitle(String title) {
        ((TextView) root.findViewById(R.id.title)).setText(title);
    }

    public void setLeftText(String text) {
        ((TextView) root.findViewById(R.id.negativeButton)).setText(text);
    }

    public void setRightText(String text) {
        ((TextView) root.findViewById(R.id.positiveButton)).setText(text);
    }

    public boolean isPositiveDismiss() {
        return isPositiveDismiss;
    }

    public void setPositiveDismiss(boolean positiveDismiss) {
        isPositiveDismiss = positiveDismiss;
    }


    private boolean isPositiveDismiss;


    public interface OnPositiveClickListener {
        public void onPositiveClickListener(Object o, DeliveryDate market);
    }

    class ViewHolder {
        TextView tv;
    }



}